﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Data;

namespace SharePointUsers
{
    class SPDAL
    {
    }
    static class SPdalHelper
    {
        public static Dictionary<string,string> PropertiesToDirctionary(object Obj, Type ObjType)
        {
            // get all public static properties of MyClass type
            PropertyInfo[] propertyInfos;
            propertyInfos = ObjType.GetProperties();
            // sort properties by name
            Array.Sort(propertyInfos,
                    delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
                    { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

            Dictionary<string, string> Values = new Dictionary<string, string>();
            // write property names
           
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                //Detect collections
                string PropName = propertyInfo.Name;
                object val = null;
                try
                {
                    SPBaseCollection list = propertyInfo.GetValue(Obj, null) as SPBaseCollection;
                    if (list != null)
                    {
                        val = "Collection of ({0}) item(s) ...";
                        if (list.Count > 0)
                            val = string.Format(val.ToString(), list.Count);
                        else
                            val = "Empty collection";
                    }
                    else
                        val = propertyInfo.GetValue(Obj, null);
                }
                catch (Exception Ex)
                {
                    //error happened
                    val = "Error fetching value. Details:" + Ex.Message;
                    if (Ex != null && Ex.InnerException != null) val += val + " Inner Error: " + Ex.InnerException.Message;
                }
                Values.Add(PropName, val==null?"":val.ToString());
                //TODO: Handle the scenario of indexed properties check: http://stackoverflow.com/questions/4268244/iterating-through-an-indexed-property-reflection

            }
            return Values;
        }
        public static SPObject PopulateUser(SPUser User)
        {
            SPObject UserObj = new SPObject(User.LoginName, User.LoginName, SPType.User);
            UserObj.Properties = SPdalHelper.PropertiesToDirctionary(User, typeof(SPUser));
            return UserObj;
        }
        public static SPObject PopulateSiteCollection(SPSite Site)
        {
            SPObject SiteObj = new SPObject(Site.PortalName, Site.ID, SPType.SiteCollection);
            SiteObj.Properties = SPdalHelper.PropertiesToDirctionary(Site, typeof(SPSite));
            return SiteObj;
        }
        public static SPObject PopulateApplication(SPWebApplication Application)
        {
            SPObject ApplicationObj = new SPObject(Application.Name, Application.Id, SPType.Application);
            ApplicationObj.Properties = SPdalHelper.PropertiesToDirctionary(Application, typeof(SPWebApplication));
            return ApplicationObj;
        }
        public static SPObject PopulateFarm(SPFarm Farm)
        {
            SPObject FarmObj = new SPObject(Farm.Name, Farm.Id, SPType.Farm);
            FarmObj.Properties = SPdalHelper.PropertiesToDirctionary(Farm, typeof(SPFarm));
            return FarmObj;
        }
    }
    public class SPReport
    {
        public string ID = "";
        public string Name = "";
        public Dictionary<string, bool> Settings = new Dictionary<string, bool>();
        public Dictionary<string, ReportProperty> Properties = new Dictionary<string, ReportProperty>();
        public bool DisplayUpdateWarning = false;
        public string UpdateWarning = "";
    }
    public class SPObject
    {
        public string Name = "";
        public Dictionary<string, string> Properties = new Dictionary<string, string>();
        public string ID = "";
        public IdentifierType IDType = IdentifierType.UNKNOWN;
        public SPType WSSType = SPType.Undefined;
        public DataTable Data = new DataTable();
        public SPObject()
        { }
        public SPObject(string name, string id, SPType wssType)
        {
            Name = name;
            ID = id;
            WSSType = wssType;
            IDType = IdentifierType.STRING;
        }
        public SPObject(string name, Guid id, SPType wssType)
        {
            Name = name;
            ID = id.ToString();
            WSSType = wssType;
            IDType = IdentifierType.GUID;
        }
    }
    public enum SPType
    {
        Farm,
        Application,
        SiteCollection,
        Site,
        List,
        Item,
        Group,
        User,
        Undefined
    }
    public enum IdentifierType
    {
        GUID,
        INT,
        STRING,
        UNKNOWN
    }
    public class ReportProperty
    {
        public ReportProperty()
        {
        }
        public ReportProperty(bool inputParameter, string defaultValue, string propertyDisplayName)
        {
            Input = inputParameter;
            Value = defaultValue;
            DisplayName = propertyDisplayName;
        }
        public bool Input = false;
        public string Value = "";
        public string DisplayName = "";
    }
}
